package cn.qzzg.springcloud.user.service.impl;

import cn.qzzg.springcloud.user.service.inter.UserServiceInter;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@Service
public class UserServiceImpl implements UserServiceInter {
    @Resource
    DiscoveryClient discoveryClient;

    @Resource
    RestTemplate restTemplate;

    @Resource
    LoadBalancerClient loadBalancerClient;

    /**
     * 通过restTemplate http请求 获取
     *
     * restTemplate.getForObject() 通过GET请求获取资源的表示形式
     * restTemplate.getForEntity() 通过GET请求获取ResponseEntity（包含状态码、请求头和响应体）
     * restTemplate.headForHeaders() 通过HEAD请求获取资源的所有请求头信息
     * restTemplate.postForObject() 通过POST请求创建新资源，并返回响应的表示形式
     * restTemplate.postForEntity() 通过POST请求创建新资源，并返回响应的表示形式
     * @return
     */
    @Override
    public String getUserName() {
        String url="http://194.0.11.50:8082/getUserName";
        String templateForObject = restTemplate.getForObject(url, String.class);
        return templateForObject;

//        String serviceName = "user-provider";
//        String result = restTemplate.getForObject("http://" + serviceName + "/getUserName", String.class);
//        return result;
    }

    /**
     * 通过restTemplate http请求 获取
     * @return
     */
    @Override
    @HystrixCommand(fallbackMethod = "getUserNameFallBack")
    public String getUserNameFail() {
        String url="http://194.0.11.50:8082/getUserNameFail";
        String templateForObject = restTemplate.getForObject(url, String.class);
        return templateForObject;
    }

    /**
     * 通过nacos远程获取
     * @return
     */
    @Override
    public String getRemoteUserName() {
//       1根据服务名称获取实例列表
        List<ServiceInstance> instances = discoveryClient.getInstances("user-provider");
        if (instances==null||instances.size()==0){
            return "没有实例";
        }
//      2手动负载均衡 获得实例
//        ServiceInstance serviceInstance = instances.get(RandomUtil.randomInt(instances.size()));  //负载均衡算法获取其中一个实例
        ServiceInstance serviceInstance = instances.get(0);
//      3 获取uri
        URI uri = serviceInstance.getUri();
        String url=uri+"/getUserName";
        String templateForObject = restTemplate.getForObject(url, String.class);
        return templateForObject;
    }

    /**
     * 通过nacos远程获取
     * @return
     */
    @Override
    public String getgetRemoteUserNameBalance() {
//      根据服务名称获取实例列表
        ServiceInstance serviceInstance=loadBalancerClient.choose("user-provider");
        if(serviceInstance==null){
            return "暂五实例";
        }
//      3 获取uri
        URI uri = serviceInstance.getUri();
        String url=uri+"/getUserName";
        String templateForObject = restTemplate.getForObject(url, String.class);
        return templateForObject;
    }

    /**
     * 请求失败返回该方法
     * @return
     */
    public String getUserNameFallBack(){
        return "getUserName3FallBack";
    }
}
